package com.cxl.parser.grab;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.Tag;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.cxl.parser.MyParser;
import com.cxl.price.bean.Good;

public abstract class GrabCommon extends FilterCommon implements GrabInfo {

	private String url;
	private String encode = "GBK";
	private int searchMaxNum=-1;//设置搜索最大数量   -1 表示不限制  其他数字表示搜索的数
	private NodeList nodeListHtml;
	
	public int getSearchMaxNum() {
		return searchMaxNum;
	}
	public void setSearchMaxNum(int searchMaxNum) {
		this.searchMaxNum = searchMaxNum;
	}
	public NodeList getNodeListHtml() {
		return nodeListHtml;
	}
	
	
	
	public void setNodeListHtml(NodeList nodeListHtml) {
		this.nodeListHtml = nodeListHtml;
	}
	
	public NodeList getNodeListHtml(String url) throws ParserException {
		Parser myParser=getParser(url);
		myParser.setEncoding(getEncode());
		NodeList htmlnodeList=null;
		try
		{
		 htmlnodeList= myParser.parse(getHtmlFilter());
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			//字符有问题
			return null;
		}
		return htmlnodeList;
	}
	
	public String getEncode() {
		return encode;
	}
	public void setEncode(String encode) {
		this.encode = encode;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) throws ParserException {
		this.url = url;
		setNodeListHtml(getNodeListHtml(url));
	}
	
	
	public Parser getParser(String url) throws ParserException {
			Parser	myParser = new MyParser(url);
		    return myParser;
	}

	/**
	 * 根据NodeFilter过滤出NodeList，node大小 获得相关需要的节点信息
	 * 
	 * @param url
	 * @param filter
	 * @param num
	 *            -1:根据node大小返回;1:返回1个node;other:返回指定数目的 前提是other数量不能大于实际size
	 * @return
	 */
	public NodeList getNodesByFilter(NodeList nodeList, NodeFilter filter, int num) {
		if(nodeList==null)
			return null;
		NodeList list = new NodeList();

		Node textnode = null;
		
			 NodeList nodes =nodeList.extractAllNodesThatMatch(filter,true);
		
			int size = nodes.size();

			if (nodes != null && nodes.size() > 0) {
				switch (num) {
				case 1:
					size = 1;// 返回一个
					break; 

				case -1:
					// node实际大小
					break; 

				default:
					size = num > size ? size : num;
					break;
				}

				for (int i = 0; i < size; i++) {
					textnode = (Node) nodes.elementAt(i);
					list.add(textnode);
				}
			}
		return list;
	}
	
	/**
	 * 根据过滤器，node大小 获得相关需要的节点信息
	 * 
	 * @param url
	 * @param filter
	 * @param num
	 *            -1:根据node大小返回;1:返回1个node;other:返回指定数目的 前提是other数量不能大于实际size
	 * @return
	 */
	public NodeList getNodesByFilter(String url, NodeFilter filter, int num) {
		NodeList list = new NodeList();

		Node textnode = null;
		try {
			Parser myParser = getParser(url);
			myParser.setEncoding(getEncode());
			NodeList nodes = myParser.extractAllNodesThatMatch(filter );
		
			int size = nodes.size();

			if (nodes != null && nodes.size() > 0) {
				switch (num) {
				case 1:
					size = 1;// 返回一个
					break; 

				case -1:
					// node实际大小
					break; 

				default:
					size = num > size ? size : num;
					break;
				}

				for (int i = 0; i < size; i++) {
					textnode = (Node) nodes.elementAt(i);
					list.add(textnode);
				}
			}
		} catch (ParserException e) {
			e.printStackTrace();
		}

		return list;
	}

	
	public String getHtmlCode(String url) {
		String htmlCode=null;
	 try {
		HttpClient httpClient = new DefaultHttpClient();
		HttpUriRequest request = new HttpGet(url);
		HttpResponse response;
		
			response = httpClient.execute(request);
		
		HttpEntity entity = response.getEntity();
		htmlCode= EntityUtils.toString(entity);
	 }catch(Exception ex)
	 {
		 ex.printStackTrace();
	 }
		return htmlCode;
	}

	public List<Good> getAllNextGoodsUrl() throws ParserException {
		List<Good> search = new ArrayList();
		NodeList htmlNodeList=getNodeListHtml();
		//得到本页商品地址
		List<Good> pagelist=searchGoodsUrl(htmlNodeList);
		search.addAll(pagelist);
		
		String next=getNextUrl(getNodeListHtml());
		
		//继续搜索  改为一页一页搜索 暂时屏蔽下面
		/**
		
		//得到下一页url地址
		String next=getNextUrl(getNodeListHtml());
		 String reg ="[\n-\r]";
		   Pattern p = Pattern.compile(reg);
		if(next!=null)
		{
		 
		   Matcher m = p.matcher(next);
		   next = m.replaceAll("");
		}
		
		while(next!=null)
		{
			//得到下一页的
			htmlNodeList=getNodeListHtml(next);
			List<String> temp=searchGoodsUrl(htmlNodeList);
			search.addAll(temp);
			if(searchMaxNum!=-1&&search.size()>searchMaxNum)
			{
				return search.subList(0,searchMaxNum);
			}
			next = getNextUrl(htmlNodeList);
			if(next!=null)
			{
				Matcher m = p.matcher(next);
			  next = m.replaceAll("");
			}
		}**/
		return search;
	}

	public String getBrand() {
		// TODO Auto-generated method stub
		return null;
	}

	public String getContent() {
		StringBuffer bufStr=new StringBuffer();
		bufStr.append("<P align=center><IMG src=\"http://img03.taobaocdn.com/imgextra/i3/69131440/T2I.8QXe8XXXXXXXXX_!!69131440.jpg\"></P>") ;
		NodeList nodelist = getNodesByFilter(getNodeListHtml(), getContentFilter(), -1);
		
		if (nodelist.size() != 0)
		{
			String content=nodelist.elementAt(0).toHtml();
			//判断图片多不多 不多久加入
			
			if(content.indexOf("<img")==-1)
			{
				try
				{
				for(String url:getOtherImgsHref())
				{
					bufStr.append("<P align=center><IMG src=\""+url+"\"></P>");
				}
				}
				catch(Exception e)
				{
					
				}
			}
			String table="<div >"+content+"</div>";
			bufStr.append(table);
		}
		
		String myDsc=""+
		"<DIV style=\"CLEAR: none; MARGIN-BOTTOM: 2em; LINE-HEIGHT: 1.4em\">"+
		"<H2 style=\"PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-SIZE: medium; PADDING-BOTTOM: 0px; MARGIN: 0px 0px 0.25em; COLOR: #e47911; PADDING-TOP: 0px\">"+
		"<HR style=\"BORDER-TOP: #999999 1px dashed; MARGIN: 3px 0px; COLOR: #ffffff; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; HEIGHT: 1px; BACKGROUND-COLOR: white; BORDER-BOTTOM-STYLE: none\">"+
		"</H2></DIV></SPAN></SPAN></STRONG><STRONG>尚生活服务承诺：</STRONG><BR>尚生活向您保证所售商品均为正品行货，自带机打发票，与商品一起寄送。凭质保证书及发票，可享受全国联保服务，与您亲临商场选购的商品享受相同的质量保证。<BR>尚生活还为您提供具有竞争力的商品价格和<A target=_blank>免运费政策</A>，请您放心购买！ </DIV></DIV>"+
		"<DIV><STRONG><FONT color=#cc3300>声明:</FONT></STRONG>因厂家会在没有任何提前通知的情况下更改产品包装、产地或者一些附件，本司不能确保客户收到的货物与商城图片、产地、附件说明完全一致。只能确保为原厂正货！并且保证与当时市场上同样主流新品一致。若本商城没有及时更新，请大家谅解！</DIV>"+
		"<DIV>&nbsp;</DIV>"+
		"<DIV><STRONG><FONT color=#804000>关于快递：合作快递和普通快递有所不同，部分快递网上无法查询订单状态，如需查询可旺旺联系我，非常乐意代劳！</FONT></STRONG></DIV>"+
		"<DIV><STRONG><FONT color=#804000>提示：本店包邮产品包邮地区仅限各城市市区，如您所在位置较偏远购买前请先咨询我替您查询下。</FONT></STRONG></DIV>";
		bufStr.append(myDsc);
		bufStr.append("<DIV align=center><IMG src=\"http://img04.taobaocdn.com/imgextra/i4/69131440/T2EDJOXeNbXXXXXXXX_!!69131440.jpg\" align=middle></DIV>");
		String reStr=bufStr.toString().replace(",", ".").replace("，", ".");
		
		String reg ="[\n-\r]";
		   Pattern p = Pattern.compile(reg);
		   Matcher m = p.matcher(reStr);
		   reStr = m.replaceAll("");
		
		   reg ="[\t]";
		   p = Pattern.compile(reg);
		  m = p.matcher(reStr);
		  reStr = m.replaceAll("");
		
		
		return reStr;
	
	}

	public String getImgHref() {
		StringBuffer text = new StringBuffer();
		NodeFilter filter = getPictureFilter();
		NodeList list = getNodesByFilter(getNodeListHtml(), filter, 1);
		if (list.size() > 0) {
			Node textnode = list.elementAt(0);
			if (textnode instanceof ImageTag) {
				{
					text.append(((ImageTag) textnode).getImageURL());
				}
			}
		}
		return text.toString();
	}

	
	public List<String> getOtherImgsHref() throws ParserException{
		List <String> imgs=new ArrayList();
		NodeFilter filter = getOtherPicturesFilter();
		if(filter==null)
		{
			return imgs;
		}
		NodeList list = getNodesByFilter(getNodeListHtml(), filter, -1);
			for(int i=0;i<list.size();i++)
			{
				Node textnode = list.elementAt(i);
				
				Tag a = (Tag) textnode;
				imgs.add(a.getAttribute("id"));
			}
		return imgs;
	}
	
	
	public String getNextUrl(NodeList htmlNodeList) {
		NodeList nodelist = getNodesByFilter(htmlNodeList, getNextFilter(), 1);
		if (nodelist.size() == 0)
			return null;
		Tag a = (Tag) nodelist.elementAt(0);
		return a.getAttribute("href");
	}

	
	/**
	 * 正则处理汉字转为数字
	 * 
	 * @param str
	 * @return
	 */
	public Double getMoney(String str) {
		Pattern p = Pattern.compile("\\d+(.\\d{3})*");
		Matcher m = p.matcher(str);
		Double db = new Double(0);
		if (m.find()) {
			db = new Double(m.group());
		}
		return db;
	}

	public Double getPrice() {
		Double db = new Double(0);
		StringBuffer text = new StringBuffer();
		try {
			NodeFilter filter = getPriceFilter();
			NodeList list = getNodesByFilter(getNodeListHtml(), filter, 1);
			if (list.size() > 0) {
				Node textnode = list.elementAt(0);
				String moneyStr = text.append(
						new String(textnode.toPlainTextString()
								.getBytes(encode))).toString();
				db = getMoney(moneyStr);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return db;
	}

	
	
	
	public String getTitle() {
		StringBuffer title = new StringBuffer();
		try {
			NodeFilter filter =getTitleFilter();
			NodeList list = getNodesByFilter(getNodeListHtml(), filter, 1);
			if (list.size() > 0) {
				Node textnode = list.elementAt(0);
				title.append(
						new String(textnode.toPlainTextString()
								.getBytes("UTF-8"))).toString();
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return title.toString();
	}

	public String getXingHao() {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean isExists(String url) {
		// TODO Auto-generated method stub
		return false;
	}

	public List<Good> searchGoodsUrl(NodeList url) {
		List<Good> list = new ArrayList<Good>();
	
			NodeFilter filter = getSearchUrlFiter();
			
			NodeList  listnode = getNodesByFilter(url, filter, -1);

			if (listnode.size() > 0) {
				for (int i = 0; i < (listnode.size()); i++) {
					Node textnode = listnode.elementAt(i);
					Tag a = (Tag) textnode;
				//	list.add(a.getAttribute("href"));
				}
			}
			
		return list;
	}
	
	/**
	 * 获得指定node对应搜索的filter 搜索的 nodelist
	 * @param node
	 * @param filter
	 * @return
	 */
	public NodeList getNextNodeListByFilter(Node node,NodeFilter filter)
	{
			NodeList nodelist=new NodeList();
			node.collectInto(nodelist,filter);
			return nodelist;
	}
	
	
	public String getShopID() {
		NodeList nodelist = getNodesByFilter(getNodeListHtml(), getShopIDFilter(), 1);
		if (nodelist.size() == 0)
			return null;
		Tag a = (Tag) nodelist.elementAt(0);
		return a.getAttribute("value");
	}
	
	
	
}
